#{extends 'CRUD/layout.html' /}
#{set title:messages.get('crud.show.title', type.modelName) /}

<div id="crudShow" class="${type.name}">

    <h2 id="crudShowTitle">&{'crud.show.title', type.modelName}</h2>

    <div class="objectForm">
    #{form action:@save(object._key()), enctype:'multipart/form-data'}
    	<p class="crudButtons">
            <input type="button" name="_addCampo" value="Agregar Campo" onclick="addCampo();"/>            
        </p>
        <div id="nuevosCampos">
        	#{list items:object.campos, as:'campo'}				
				<div class="crudField crud_text">
					<label for="object_nombre">
				    ${campo.nombre}
    				</label>
    				<input id="campo.${campo.id}" class="" type="text" name="campo.${campo.id}" size="50" value="${campo.valor}">
				</div>
			#{/list}
        </div>
        #{crud.form }
        	#{crud.custom 'organizaciones'}
	    	  	#{customs.customSubbaseOrganizaciones 
	    	  		organizaciones: organizaciones, 	    	  		
	    	  		field:object /}
	    	#{/crud.custom}
	    	#{crud.custom 'tipoSubbase'}
	    	  	#{customs.customTipoSubbase 
	    	  		tipoSubbases: tipoSubbases, 
	    	  		field:object /}
	    	#{/crud.custom}
        #{/crud.form}
        <p class="crudButtons">
            <input type="submit" name="_save" value="&{'crud.save', type.modelName}" />            
        </p>
        
    <div id="dialog-Subbases"  style="display: none" class="dialog-style">
		<h2 id="crudShowTitle">&{'crud.blank.title', 'Sub-Bases'}
		<input type="text" id="subbaseNameSearch" name="subbaseNameSearch">
		</h2>
		<div class="objectForm">
			<div class="crudField crud_text" id="subbaseDiv">
				<table id="subbaseTable">			
					#{list items:subbases, as:'subbase'}				
						<tr><td><a href="@{SubBases.importarSubbase(object._key(), subbase.id)}" class="organizacion" >${subbase.nombre}</a></td></tr>
					#{/list}
				</table>
			</div>
		</div>	
	</div> 
    #{/form}
    </div>
	
	
	
	
	Importar desde Subbase Existente <img id="importarSubbase" src="/public/images/Add.png" alt="Add">
		
	#{form @SubBases.exportar(object._key()), enctype:'multipart/form-data'}
		<input type="hidden" id="sector_Filter" name="sector_Filter" value="${sector_Filter}">
		<input type="hidden" id="rama_Filter" name="rama_Filter" value="${rama_Filter}">
		<input type="hidden" id="rubro_Filter" name="rubro_Filter" value="${rubro_Filter}">
		<input type="hidden" id="subrubro_Filter" name="subrubro_Filter" value="${subrubro_Filter}">
		<input type="hidden" id="tratamiento_Filter" name="tratamiento_Filter" value="${tratamiento_Filter}">
		<input type="hidden" id="contacto_Filter" name="contacto_Filter" value="${contacto_Filter}">
		<input type="hidden" id="ejecutivoNivel1_Filter" name="ejecutivoNivel1_Filter" value="${ejecutivoNivel1_Filter}">
		<input type="hidden" id="ejecutivoNivel2_Filter" name="ejecutivoNivel2_Filter" value="${ejecutivoNivel2_Filter}">
		<input type="hidden" id="ejecutivoNivel3_Filter" name="ejecutivoNivel3_Filter" value="${ejecutivoNivel3_Filter}">
		<input type="hidden" id="domicilio_Filter" name="domicilio_Filter" value="${domicilio_Filter}">
		<input type="hidden" id="piso_Filter" name="piso_Filter" value="${piso_Filter}">
		<input type="hidden" id="oficina_Filter" name="oficina_Filter" value="${oficina_Filter}">
		<input type="hidden" id="localidad_Filter" name="localidad_Filter" value="${localidad_Filter}">
		<input type="hidden" id="web_Filter" name="web_Filter" value="${web_Filter}">
		<input type="hidden" id="mail_Filter" name="mail_Filter" value="${mail_Filter}">
		<input type="hidden" id="telefono_Filter" name="telefono_Filter" value="${telefono_Filter}">
		<input type="hidden" id="celular_Filter" name="celular_Filter" value="${celular_Filter}">
		<input type="hidden" id="fax_Filter" name="fax_Filter" value="${fax_Filter}">
		<input type="hidden" id="observaciones_Filter" name="observaciones_Filter" value="${observaciones_Filter}">
		   
        <p class="crudButtons">
            <input type="submit" value="Exportar" />
        </p>
    #{/form}
    
    #{form action:@delete(object._key()), enctype:'multipart/form-data'}
        <p class="crudDelete">
            <input type="submit" value="&{'crud.delete', type.modelName}" />
        </p>
    #{/form}

</div>

<div id="dialog-nuevo-campo"  style="display: none" class="dialog-style" >
		<h2 id="crudShowTitle">&{'crud.blank.title', 'Campo'}
		</h2>
		<div id="nuevoCampoError"></div>
		<div class="objectForm">
			<div class="crudField crud_text">
				<label for = "campoNombre">Nombre</label><input type="text" id="campoNombre" name="campoNombre" value="">
			</div>
			<div class="crudField crud_text">
				<label for = "campoValor">Valor</label><input type="text" id="campoValor" name="campoValor" value="">
			</div>
			<input type="button" value="Save" onclick="saveNuevoCampo();">
		</div>		
	</div> 
	
<script type="text/javascript">
$().ready(function() {
	$( "#dialog-nuevo-campo").dialog({
		autoOpen: false,
	    width: 550, 
	    maxWidth: 550,
	    minWidth: 430,
		height: 'auto',
		fluid: true, 
	    resizable: false,
	    dialogClass: "dialog-style",
	    open: function(event, ui){
	    },
	    close: function(event, ui){
	    	$( "#dialog-nuevo-campo" ).dialog( "close" );
	    }
	});
	$( "#dialog-Subbases").dialog({
		autoOpen: false,
	    width: 550, 
	    maxWidth: 550,
	    minWidth: 430,
		height: 'auto',
		fluid: true, 
	    resizable: false,
	    dialogClass: "dialog-style",
	    open: function(event, ui){
	    },
	    close: function(event, ui){
	    	$( "#dialog-Subbases" ).dialog( "close" );
	    }
	});
	
	$("#subbaseNameSearch").keyup(function() {
		  searchSubbases();
	});
	$( "#importarSubbase" ).click(function() {
		$( "#dialog-Subbases" ).dialog( "open" );
		return false;
	});
})

function addCampo(){
	$( "#dialog-nuevo-campo" ).dialog( "open" );
}

function saveNuevoCampo(){
	campoNombre = $("#campoNombre").val();
	campoValor = $("#campoValor").val();
	$.get("/Subbases/newCampo",  {
			id: '${object._key()}',
			nombre: campoNombre,		
			valor: campoValor
		},function(data){
			if(data[0] != 'ERROR'){
				window.location.assign("/admin/subbases/${object._key()}");
				return false;
			}else {
				alert(data[1]);
				return false;
			}
			
		},'json');
	}


function searchSubbases(id){
	$.get("/SubBases/search",  {		
		nombre: $("#subbaseNameSearch").val()
	},function(data, id){
		if(data[0] != 'ERROR'){
			$("#subbaseTable").remove();
			var subbases = JSON.parse(data[1]);
			var i = 0;
			html = "<table id='subbaseTable'>";
			for(i = 0; i < subbases.length; i++) {
				html = html + 
				"<tr><td><a href='/SubBases/importarSubbase?subBaseId="+ ${object._key()}+"&subbaseSelected="+subbases[i].id+"' class='subbase' >"+
					' ' + subbases[i].nombre +
						"</a></td></tr>";
			}
			html = html + "</table>";
			$("#subbaseDiv").append(html);
			return false;
		}else {
			return false;
		}
		
	},'json');
}

 	$("#object_fechaFinal").datepicker({
		 defaultDate: "+1w",
		 changeMonth: true,
		 numberOfMonths: 1,
		 dateFormat: 'yy-mm-dd'
	 });
</script>